* load IQ data from:
* 		Lynn & Meisenberg, Intelligence (2010) 108 IQs 
*		Wichert et al., Intelligence (2010) African IQ data (Mean IQ from Table 5)
*		Lynn & Vanhanon 185 country estimated IQ (http://www.rlynn.co.uk/uploads/pdfs/Intelligence%20and%20the%20Wealth%20and%20Poverty%20of%20Nations.pdf)

cd "~/Documents/econ/research/current/cognitive/estimation/data"
import delimited using iq_lynn2010.txt, clear
gen byte estimated = (iqqu==.)
kountry country, from(other) stuck
rename _ISO3N_ iso3n
kountry iso3n, from(iso3n) to(iso3c)
rename _ISO3C_ countrycode
drop iso3n
saveold iq_lynn2010, replace version(12)

drop if countrycode==""
rename iq iq_lynn
drop if estimated
keep countrycode iq_lynn 
order countrycode iq_lynn
sort countrycode
merge 1:1 countrycode using iq185
drop _merge
merge 1:1 countrycode using iq_Wicherts
drop _merge
gen year = 2010

gen iq = iq_lynn
replace iq = iq_Wicherts if iq_Wicherts!=.
gen iq185new = iq185
replace iq185new = iq if iq!=.
// revise estimated IQ in iq185 with Wicherts Africa data
replace iq185new = (iq["NAM":iso]+iq["ZMB":iso]+iq["COD":iso])/3 if countrycode=="AGO"
replace iq185new = (iq["COD":iso]+iq["TZA":iso]+iq["UGA":iso])/3 if countrycode=="BDI"
replace iq185new = (iq["GHA":iso]+iq["NGA":iso])/2 if countrycode=="BEN"
replace iq185new = iq["GHA":iso] if countrycode=="BFA"
replace iq185new = iq["GHA":iso] if countrycode=="CIV"
replace iq185new = (iq["NGA":iso]+iq["COG":iso])/2 if countrycode=="CMR"
replace iq185new = iq["COG":iso] if countrycode=="GAB"
replace iq185new = iq["SEN":iso] if countrycode=="GIN"
replace iq185new = iq["SEN":iso] if countrycode=="GMB"
replace iq185new = iq["SEN":iso] if countrycode=="GNB"
replace iq185new = (iq["NGA":iso]+iq["CAF":iso]+iq["COG":iso])/3 if countrycode=="GNQ"
replace iq185new = iq["SEN":iso] if countrycode=="LBR"
replace iq185new = (iq["ZAF":iso]+iq["ZWE":iso])/2 if countrycode=="LSO"
replace iq185new = (iq["SEN":iso]+iq["MLI":iso])/2 if countrycode=="MRT"
replace iq185new = (iq["MLI":iso]+iq["NGA":iso])/2 if countrycode=="NER"
replace iq185new = (iq["COD":iso]+iq["TZA":iso]+iq["UGA":iso])/3 if countrycode=="RWA"
replace iq185new = (iq["EGY":iso]+iq["ETH":iso]+iq["KEN":iso])/3 if countrycode=="SDN"
replace iq185new = (iq["ETH":iso]+iq["KEN":iso])/2 if countrycode=="SOM"
replace iq185new = (iq["NGA":iso]+iq["CAF":iso]+iq["COG":iso])/3  if countrycode=="STP"
replace iq185new = (iq["ZAF":iso]+iq["ZWE":iso])/2 if countrycode=="SWZ"
replace iq185new = (iq["NGA":iso]+iq["CAF":iso])/2 if countrycode=="TCD"
replace iq185new = (iq["GHA":iso]+iq["NGA":iso])/2 if countrycode=="TGO"

label var iq "IQ"
label var iq185 "IQ"

saveold iq, replace version(11)
